String comandoClient = "";
int pin2 = 2,
    pin3 = 3,
    pin4 = 4,
    pin5 = 5,
    pin6 = 6,
    pin7 = 7,
    pin8 = 8,
    pin9 = 9,
    pin10 = 10,
    pin11 = 11,
    pin12 = 12,
    pin13 = 13;

void setup(){
  Serial.begin(9600);
  initPortas();
  initDefaut();
  Serial.println("Servidor inicializado.");

}

void initPortas(){
  pinMode(pin2, OUTPUT);
  pinMode(pin3, OUTPUT);
  pinMode(pin4, OUTPUT);
  pinMode(pin5, OUTPUT);
  pinMode(pin6, OUTPUT);
  pinMode(pin7, OUTPUT);
  pinMode(pin8, OUTPUT);
  pinMode(pin9, OUTPUT);
  pinMode(pin10, OUTPUT);
  pinMode(pin11, OUTPUT);
  pinMode(pin12, OUTPUT);
  pinMode(pin13, OUTPUT);
}

void initDefaut(){
  digitalWrite(pin2, LOW);
  digitalWrite(pin3, LOW);
  digitalWrite(pin4, LOW);
  digitalWrite(pin5, LOW);
  digitalWrite(pin6, LOW);
  digitalWrite(pin7, LOW);
  digitalWrite(pin8, LOW);
  digitalWrite(pin9, LOW);
  digitalWrite(pin10, LOW);
  digitalWrite(pin11, LOW);
  digitalWrite(pin12, LOW);
  digitalWrite(pin13, LOW);
}

void loop(){
    
    while (Serial.available()) {
      
        char c = Serial.read(); 
        comandoClient += c;
      
        
        if (c == '?') {
          
          int spacePosition = comandoClient.indexOf(':');     
          String comando =  comandoClient.substring(0,spacePosition);
          String porta = comandoClient.substring((spacePosition+1),(comandoClient.length()-1));
          execultarComandos(comando,porta);
          
        }else if(c == '$'){
          if(comandoClient.equals("testar-conexao$")){
            Serial.write("True");
            comandoClient = "";
          }
        }
      }
}

void execultarComandos(String comando, String porta){
    if(comando.equals("on")){
      comandoOn(porta);
    }else if(comando.equals("off")){
      comandoOff(porta); 
    }
    comandoClient = "";
}

void comandoOn(String porta){
  if(porta.equals("2")){
    digitalWrite(pin2, HIGH);
  }else if(porta.equals("3")){
    digitalWrite(pin3, HIGH);
  }else if(porta.equals("4")){
    digitalWrite(pin4, HIGH);
  }else if(porta.equals("5")){
    digitalWrite(pin5, HIGH);
  }else if(porta.equals("6")){
    digitalWrite(pin6, HIGH);
  }else if(porta.equals("7")){
    digitalWrite(pin7, HIGH);
  }else if(porta.equals("8")){
    digitalWrite(pin8, HIGH);
  }else if(porta.equals("9")){
    digitalWrite(pin9, HIGH);
  }else if(porta.equals("10")){
    digitalWrite(pin10, HIGH);
  }else if(porta.equals("11")){
    digitalWrite(pin11, HIGH);
  }else if(porta.equals("12")){
    digitalWrite(pin12, HIGH);
  }else if(porta.equals("13")){
    digitalWrite(pin13, HIGH);
  } 
  
}


void comandoOff(String porta){
  if(porta.equals("2")){
    digitalWrite(pin2, LOW);
  }else if(porta.equals("3")){
    digitalWrite(pin3, LOW);
  }else if(porta.equals("4")){
    digitalWrite(pin4, LOW);
  }else if(porta.equals("5")){
    digitalWrite(pin5, LOW);
  }else if(porta.equals("6")){
    digitalWrite(pin6, LOW);
  }else if(porta.equals("7")){
    digitalWrite(pin7, LOW);
  }else if(porta.equals("8")){
    digitalWrite(pin8, LOW);
  }else if(porta.equals("9")){
    digitalWrite(pin9, LOW);
  }else if(porta.equals("10")){
    digitalWrite(pin10, LOW);
  }else if(porta.equals("11")){
    digitalWrite(pin11, LOW);
  }else if(porta.equals("12")){
    digitalWrite(pin12, LOW);
  }else if(porta.equals("13")){
    digitalWrite(pin13, LOW);
  }
}


